﻿using Dapper;
using System;
using BCEntity.OA.WorkSchedule.WorkScheduleChildrenTask;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;

namespace BCData.OA.WorkSchedule.WorkScheduleChildrenTask
{
	/// <summary>
	/// 工作进度表子任务
	/// </summary>
	public class WorkScheduleChildrenTaskData : IWorkScheduleChildrenTaskData
	{
		private readonly IDatabaseContext databaseContext;
		private const string QUERY_EXISTS = @"SELECT 1 FROM oa_workschedulechildrentask";
		private const string QUERY = @"SELECT WorkScheduleChildrenTaskId,EndDate,StartDateTime,EndDateTime,CreateTime,CreateAdminId,CreateAdminName,EditAdminId,EditAdminName,UpdateTime,
											WorkScheduleId,CompanyId,DepartmentId,ChildTaskTitle,ChildType,ChildTaskRemark,ProcessStatus,StartDate,CompanyName,DepartmentName
										FROM oa_workschedulechildrentask";
		public WorkScheduleChildrenTaskData(IDatabaseContext databaseContext)
		{
			this.databaseContext = databaseContext;
		}
		#region 检查工作进度表子任务是否存在
		public async Task<bool> Exists(long workScheduleChildrenTaskId)
		{
			string sql = string.Format(@"{0} WHERE WorkScheduleChildrenTaskId=?WorkScheduleChildrenTaskId", QUERY_EXISTS);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorkScheduleChildrenTaskId", workScheduleChildrenTaskId, DbType.Int64);
			return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
		}
		#endregion

		#region 添加工作进度表子任务
		public async Task<WorkScheduleChildrenTaskEntity> Add(WorkScheduleChildrenTaskEntity entity)
		{
			string sql = SqlCommands.GetInsertSql("oa_workschedulechildrentask", new string[] { "CreateAdminId", "CreateAdminName", "WorkScheduleId", "CompanyId", "DepartmentId", "CompanyName", "DepartmentName", "ChildTaskTitle", "ChildType", "ChildTaskRemark", "StartDate", "EndDate", "StartDateTime", "EndDateTime" });
			sql = string.Format("{0} {1} WHERE WorkScheduleChildrenTaskId=LAST_INSERT_ID();", sql, QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?CreateAdminId", entity.CreateAdminId, DbType.Int64, size: 20);
			parameters.Add("?CreateAdminName", entity.CreateAdminName?.Trim(), DbType.AnsiString, size: 20);
			parameters.Add("?WorkScheduleId", entity.WorkScheduleId, DbType.Int64, size: 20);
			parameters.Add("?CompanyId", entity.CompanyId, DbType.Int32, size: 11);
			parameters.Add("?DepartmentId", entity.DepartmentId, DbType.Int32, size: 11);
			parameters.Add("?CompanyName", entity.CompanyName?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?DepartmentName", entity.DepartmentName?.Trim(), DbType.AnsiString, size: 100);
			parameters.Add("?ChildTaskTitle", entity.ChildTaskTitle?.Trim(), DbType.AnsiString, size: 100);
			parameters.Add("?ChildType", entity.ChildType.ToString(), DbType.AnsiString);
			parameters.Add("?ChildTaskRemark", entity.ChildTaskRemark?.Trim(), DbType.String);
			parameters.Add("?StartDate", entity.StartDate, DbType.DateTime);
			parameters.Add("?EndDate", entity.EndDate, DbType.DateTime);
			parameters.Add("?StartDateTime", entity.StartDateTime, DbType.DateTime);
			parameters.Add("?EndDateTime", entity.EndDateTime, DbType.DateTime);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<WorkScheduleChildrenTaskEntity>(sql, parameters, databaseContext.Transaction);

		}
		#endregion

		#region 修改工作进度表子任务
		public async Task<WorkScheduleChildrenTaskEntity> Update(WorkScheduleChildrenTaskEntity entity)
		{
			string sql = SqlCommands.GetUpdateSql("oa_workschedulechildrentask", new string[] { "EndDate", "StartDateTime", "EndDateTime", "EditAdminId", "EditAdminName", "WorkScheduleId", "CompanyName", "DepartmentName", "CompanyId", "DepartmentId", "ChildTaskTitle", "ChildType", "ChildTaskRemark", "ProcessStatus", "StartDate" }, new string[] { "WorkScheduleChildrenTaskId" });
			sql = string.Format("{0} {1} WHERE WorkScheduleChildrenTaskId=?WorkScheduleChildrenTaskId;", sql, QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorkScheduleChildrenTaskId", entity.WorkScheduleChildrenTaskId, DbType.Int64, size: 20);
			parameters.Add("?EndDate", entity.EndDate, DbType.DateTime);
			parameters.Add("?StartDateTime", entity.StartDateTime, DbType.DateTime);
			parameters.Add("?EndDateTime", entity.EndDateTime, DbType.DateTime);
			parameters.Add("?EditAdminId", entity.EditAdminId, DbType.Int64, size: 20);
			parameters.Add("?EditAdminName", entity.EditAdminName?.Trim(), DbType.AnsiString, size: 20);
			parameters.Add("?WorkScheduleId", entity.WorkScheduleId, DbType.Int64, size: 20);
			parameters.Add("?CompanyId", entity.CompanyId, DbType.Int32, size: 11);
			parameters.Add("?DepartmentId", entity.DepartmentId, DbType.Int32, size: 11);
			parameters.Add("?CompanyName", entity.CompanyName?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?DepartmentName", entity.DepartmentName?.Trim(), DbType.AnsiString, size: 100);
			parameters.Add("?ChildTaskTitle", entity.ChildTaskTitle?.Trim(), DbType.AnsiString, size: 100);
			parameters.Add("?ChildType", entity.ChildType.ToString(), DbType.AnsiString);
			parameters.Add("?ChildTaskRemark", entity.ChildTaskRemark?.Trim(), DbType.String);
			parameters.Add("?ProcessStatus", entity.ProcessStatus.ToString(), DbType.AnsiString);
			parameters.Add("?StartDate", entity.StartDate, DbType.DateTime);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<WorkScheduleChildrenTaskEntity>(sql, parameters, databaseContext.Transaction);
		}
		#endregion

		#region 删除工作进度表子任务
		public async Task<bool> Delete(long workScheduleChildrenTaskId)
		{
			string sql = @"DELETE FROM oa_workschedulechildrentask WHERE WorkScheduleChildrenTaskId=?WorkScheduleChildrenTaskId";
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorkScheduleChildrenTaskId", workScheduleChildrenTaskId, DbType.Int64);
			return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
		}
		#endregion

		#region 获取工作进度表子任务
		public async Task<WorkScheduleChildrenTaskEntity> GetEntity(long workScheduleChildrenTaskId)
		{
			string sql = string.Format(@"{0} WHERE WorkScheduleChildrenTaskId=?WorkScheduleChildrenTaskId", QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorkScheduleChildrenTaskId", workScheduleChildrenTaskId, DbType.Int64);
			return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<WorkScheduleChildrenTaskEntity>(sql, parameters);
		}
		#endregion

		#region 获取工作进度表子任务列表
		public async Task<IEnumerable<WorkScheduleChildrenTaskEntity>> GetList(long workScheduleId)
		{
			string sql = string.Format(@"{0} WHERE WorkScheduleId=?WorkScheduleId", QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorkScheduleId", workScheduleId, DbType.Int64, size: 20);
			return await this.databaseContext.Slave.QueryAsync<WorkScheduleChildrenTaskEntity>(sql, parameters);
		}
		#endregion

		#region 获取工作进度表子任务分页
		public async Task<(IEnumerable<WorkScheduleChildrenTaskEntity>, int)> Query(DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
		{
			List<string> wheres = new List<string>();
			DynamicParameters parameters = new DynamicParameters();

			if (start.HasValue && !end.HasValue)
			{
				wheres.Add("CreateTime>=?Start");
				parameters.Add("?Start", start.Value, DbType.DateTime);
			}
			else if (!start.HasValue && end.HasValue)
			{
				wheres.Add("CreateTime<=?End");
				parameters.Add("?End", end.Value, DbType.DateTime);
			}
			else if (start.HasValue && end.HasValue)
			{
				wheres.Add("CreateTime>=?Start AND CreateTime<=?End");
				parameters.Add("?Start", start.Value, DbType.DateTime);
				parameters.Add("?End", end.Value, DbType.DateTime);
			}
			string sql = string.Format(@"{0} {1} ORDER BY {4} {5} LIMIT {2},{3}; SELECT COUNT(1) FROM oa_workschedulechildrentask {1}",
				  QUERY,
				  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
				  (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize,
				  pageSize,
				  string.IsNullOrWhiteSpace(sortName) ? "CreateTime" : sortName,
				  ascending.HasValue && ascending.Value ? "ASC" : "DESC");
			return await this.databaseContext.Slave.QueryMultipleAsync<WorkScheduleChildrenTaskEntity, int>(sql, parameters);
		}
		#endregion
	}
}



